home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / pdsrt321.zip / KEYSRT.C < prev    next >
C/C++ Source or Header  |  1991-09-07  |  4KB  |  176 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <alloc.h>
  5.  
  6. #include "queue.h"
  7.  
  8. #define _32K 32768U
  9.  
  10. static FILE *kin, *kout;
  11. static QUE_DEF *K_Keys;
  12. static char K_OutName[65] = "";
  13. static char KeyName[65];
  14. static int K_RecLen;
  15. static char *K_Buffer;
  16. static long where;
  17.  
  18. struct KeyEntry {
  19.     int             Begin;
  20.     int             Len;
  21.     char            Order;
  22.     char            Case;
  23.     char            Type;
  24.     };
  25. extern char IntPath[65];
  26.  
  27.  void
  28. GetKeys (void) {
  29.     extern FILE *fin, *fout;
  30.     extern QUE_DEF *Keys;
  31.     extern int RecLen;
  32.     extern char *Buffer;
  33.     extern char OutName[65];
  34.     extern unsigned long lnno;
  35.     extern unsigned long EndAdr;
  36.     unsigned long BufSize;
  37.  
  38.     QUE_ENTRY *t;
  39.     struct KeyEntry *t1, *t2;
  40.     int Begin = 8;
  41.  
  42.     kin = fin; kout = fout;
  43.     K_Keys = Keys;
  44.     strcpy(K_OutName, OutName);
  45.     strcpy(KeyName, IntPath);
  46.     if ( (KeyName[0] != '\0') && (KeyName[strlen(KeyName)-1] != '\\') )
  47.         strcat(KeyName, "\\");
  48.     strcat(KeyName, "SORT.K$$");
  49.     strcpy(OutName, KeyName);
  50.     K_RecLen = RecLen;
  51.     Keys = malloc(sizeof(QUE_DEF));
  52.     InitQueue(Keys);
  53.     BufSize = (coreleft() - 2048) / 2;
  54.     if (BufSize > _32K) setvbuf(kin, NULL, _IOFBF, _32K);
  55.     else setvbuf(kin, NULL, _IOFBF, (size_t) BufSize);
  56.  
  57.  
  58.     for (t=K_Keys->Head; t != NULL; t = t->Next) {
  59.         t1 = (struct KeyEntry *) t->Body;
  60.         t2 = malloc(sizeof(struct KeyEntry));
  61.         t2->Begin = Begin;
  62.         t2->Len = t1->Len;
  63.         Begin += t1->Len;
  64.         t2->Order = t1->Order;
  65.         t2->Case = t1->Case;
  66.         t2->Type = t1->Type;
  67.         Enque(Keys, t2);
  68.         }
  69.  
  70.     if ( (K_Buffer = malloc(Begin + 1)) == NULL ) {
  71.         fprintf(stderr, "Insufficient memory for Key file buffer.\n");
  72.         exit(14);
  73.         }
  74.  
  75.     if ( (fin = fopen(KeyName, "w")) == NULL ) {
  76.         fprintf(stderr, "I can't create key file: %s", KeyName);
  77.         perror("");
  78.         exit(13);
  79.         }
  80.     if (BufSize > _32K) setvbuf(fin, NULL, _IOFBF, _32K);
  81.     else setvbuf(fin, NULL, _IOFBF, (size_t) BufSize);
  82.  
  83.     while (where = ftell(kin), fgets(Buffer, RecLen + 2, kin) != NULL ) {
  84.         lnno++;
  85.         if (Buffer[strlen(Buffer) - 1] != '\n') {
  86.             fprintf(stderr, "Record #%lu exceeds maximum length %d\n",
  87.                     lnno, RecLen);
  88.             exit(4);
  89.             }
  90.         sprintf(K_Buffer, "%07ld", where);
  91.         K_Buffer[7] = '|';
  92.         K_Buffer[8] = '\0';
  93.         for (t=K_Keys->Head; t != NULL; t = t->Next) {
  94.             t1 = (struct KeyEntry *) t->Body;
  95.             strncat(K_Buffer, &Buffer[t1->Begin], t1->Len);
  96.             }
  97.         errno = 0;
  98.         fprintf(fin, "%s\n", K_Buffer);
  99.         if (errno) {
  100.             perror("I/O error on key file");
  101.             exit(8);
  102.             }
  103.         }
  104.     lnno = 0;
  105.  
  106.     fclose(kin);
  107.     fclose(fin);
  108.     RecLen = Begin;
  109.  
  110.     if ( (fin = fopen(KeyName, "r")) == NULL ) {
  111.         fprintf(stderr, "Major system failure!!\n");
  112.         exit(20);
  113.         }
  114.     fseek(fin, 0, SEEK_END);
  115.     EndAdr = ftell(fin);
  116.     fseek(fin, 0,SEEK_SET);
  117.  
  118.     }
  119.  
  120.  void
  121. PutKeys (void) {
  122.     extern FILE *fin, *fout;
  123.     extern int RecLen;
  124.     extern char *Buffer;
  125.     extern char InputName[65];
  126.  
  127.     FILE *F1, *F2;
  128.     char *p;
  129.     long BufSize;
  130.  
  131.     BufSize = coreleft() / 2;
  132.     if ( (F2 = fopen(K_OutName, "w")) == NULL ) {
  133.         fprintf(stderr, "I can't create output file: %s\n", K_OutName);
  134.         exit(7);
  135.         }
  136.     if (BufSize > _32K) setvbuf(F2, NULL, _IOFBF, _32K);
  137.     else setvbuf(F2, NULL, _IOFBF, (size_t) BufSize);
  138.  
  139.     if ( (kin = fopen(InputName, "r")) == NULL ) {
  140.         fprintf(stderr, "I can't find input file: %s", InputName);
  141.         perror("");
  142.         exit(2);
  143.         }
  144.  
  145.     if ( (F1 = fopen(KeyName, "r")) == NULL ) {
  146.         fprintf(stderr, "%s\n", KeyName);
  147.         fprintf(stderr, "Major error! %d", errno);
  148.         perror("");
  149.         exit(11);
  150.         }
  151.     if (BufSize > _32K) setvbuf(F1, NULL, _IOFBF, _32K);
  152.     else setvbuf(F2, NULL, _IOFBF, (size_t) BufSize);
  153.  
  154.     while (fgets(K_Buffer, RecLen + 2, F1) != NULL ) {
  155.         where = strtol(K_Buffer, &p, 10);
  156.         p = strchr(K_Buffer, '|') + 1;
  157.         errno = 0;
  158.         fseek(kin, where, SEEK_SET);
  159.         fgets(Buffer, K_RecLen + 2, kin);
  160.         if (errno) {
  161.             perror("I/O error on input file.");
  162.             exit(8);
  163.             }
  164.         fputs(Buffer, F2);
  165.         if (errno) {
  166.             perror("I/O error on output file.");
  167.             exit(8);
  168.             }
  169.         }
  170.  
  171.     fclose(kin);
  172.     fclose(fout);
  173.     fclose(F1);
  174.     unlink("SORT.K$$");
  175.     }
  176.